package models

import (
	"fmt"

	_ "github.com/go-sql-driver/mysql"
    "github.com/jinzhu/gorm"

    "huaxrobot/config"

)

var db *gorm.DB
var dberr error

func InitDB()  {
    //读取配置信息
    driverName := config.CONFIG.GetString("db.mysql.type")
    host := config.CONFIG.GetString("db.mysql.host")
    port := config.CONFIG.GetString("db.mysql.port")
    database := config.CONFIG.GetString("db.mysql.database")
    username := config.CONFIG.GetString("db.mysql.username")
    password := config.CONFIG.GetString("db.mysql.password")
    charset := "utf8"
    args := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=%s&parseTime=true",
        username,
        password,
        host,
        port,
        database,
        charset,
    )

    db, dberr = gorm.Open(driverName, args)
    //db, err := gorm.Open("mysql", "user:islot@/blog?charset=utf8&parseTime=True&loc=Local")
    if dberr != nil {
        fmt.Println("failed to connect database,err:", dberr)
    } 
    db.AutoMigrate(&PlanCase{})
    db.AutoMigrate(&PlanStatistics{})
    db.AutoMigrate(&PerformanceMs{})
    db.AutoMigrate(&PerformanceConfig{})
}